import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../model/repay_plan_model.dart';

class RepayPlansController extends GetxController {

  var loanNum = ''; // 订单编号
  var repayPlanList = RxList<RepayPlan>().obs;

  var isChoose = false.obs;

  @override
  void onInit() {
    super.onInit();
    loanNum = Get.arguments['loan_num'];
  }

  @override
  void onReady() {
    super.onReady();

    /// 拉取还款计划
    getRepayPlans();
  }

  /// 拉取还款计划
  void getRepayPlans() {
    var temp = [
      RepayPlan(qs: 1,money: '999',repayTime: '2024-02-02',status:1,isCheck: true),
      RepayPlan(qs: 2,money: '999',repayTime: '2024-02-02',status:2),
      RepayPlan(qs: 3,money: '999',repayTime: '2024-02-02',status:3),
      RepayPlan(qs: 4,money: '999',repayTime: '2024-02-02',status:4),
      RepayPlan(qs: 5,money: '999',repayTime: '2024-02-02',status:5),
      RepayPlan(qs: 6,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 7,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 8,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 9,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 10,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 11,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
      RepayPlan(qs: 12,money: '999',repayTime: '2024-02-02',status:1),
    ];

    for (var element in temp) {
      element.isCheck = element.status == 2 || element.status == 4;
      repayPlanList.value.add(element);
    }
  }

  /// 结清
  void settled(){
    if(isChoose.value){
      isChoose.value = false;

      var temp = <RepayPlan>[];
      for (var element in repayPlanList.value) {
        element.isCheck = element.status == 2 || element.status == 4;
        temp.add(element);
      }
      repayPlanList.value.clear();
      repayPlanList.value.addAll(temp);
    }else{
      EasyLoading.showToast('结清');

//       vm.getTrial(orderId, selectBean!!.period_no, object : CallbackT<RepayTrialTempBean> {
//       override fun result(data: RepayTrialTempBean) {
//       if (data.need_change_card == 1){
//       "请重新绑定银行卡".showShort()
// //                    JumpCenter.toAddBank(orderId)
//       } else {
//       showHintDialog(data.remark)
//       }
//       }
//       })
//
//       private fun showHintDialog(remark:String) {
//         val dialog = CustomDialog(this)
//         dialog.setMsg("本次还款${remark}，是否继续？请注意：确认后将会扣款")
//         dialog.addListener(object : CustomDialog.OnClickListener {
//         override fun confirm(dialog: AlertDialog?) {
//         super.confirm(dialog)
//         repay()
//         }
//         })
//         dialog.show()
//       }
//
//       fun repay(){
//         vm.getRepay(orderId,selectBean!!.period_no,object : Callback {
//         override fun result(vararg data: Any) {
//         if (data[0] as Int == 200){
//         showErrorDialog("还款成功",object :CustomDialog.OnClickListener{
//         override fun confirm(dialog: AlertDialog?) {
//         super.confirm(dialog)
//         setResult(Activity.RESULT_OK)
//         onBackPressed()
//         }
//         })
//         }else if (data[0] as Int == -200){
//         showErrorDialog("${data[1]}",object :CustomDialog.OnClickListener{
//         override fun confirm(dialog: AlertDialog?) {
//         super.confirm(dialog)
//         onBackPressed()
//         }
//         })
//         }
//         }
//         })
//       }
    }
  }

  /// 还款
  void repayment(){
    if(isChoose.value){
      if(selectRepayPlan == null){
        EasyLoading.showToast('请选择还款期数');
      }else{
        EasyLoading.showToast('${selectRepayPlan!.qs}');
      }
    }else{
      isChoose.value = true;
    }
    (repayPlanList.value[3]).isCheck = true;
  }

  /// 选择还款的某个期数
  RepayPlan? selectRepayPlan;
  check(RepayPlan bean) {
    if(bean.status == 2 || bean.status == 4){
      return;
    }

    selectRepayPlan = bean;
    var temp = <RepayPlan>[];
    for (var element in repayPlanList.value) {
      element.isCheck = element.status == 2 || element.status == 4 || bean.qs==element.qs;
      temp.add(element);
    }
    repayPlanList.value.clear();
    repayPlanList.value.addAll(temp);
  }
}
